package com.aranandroid.customview.progress

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View
import com.aranandroid.customview.R

/**
 * Created by Chen on 2017/6/4.
 * 圆形数字进度条
 */
class LineProgresView @JvmOverloads constructor(
    context: Context?,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {

    //进度
    private var mProgress = 0
    
    //圆角
    private var mRadius = 4

    //文字的大小
    private var mTextSize = 0

    // 字体加粗斜体
    private var mTextStyle = 0

    //文字颜色
    private var mTextColor = 0

    //进度条的颜色
    private var mWaveColor = 0

    /**
     * 线条宽度
     */
    private var mStrokeWidth = 5

    // 画文字的笔
    private var mTextPaint: Paint? = null

    // 画进度条的笔
    private var mProgressPaint: Paint? = null

    // 文字的区域
    private var mTextRect: Rect? = null

    var viewWidth = 0
    var viewHeight:Int = 0


    private fun getAttrs(attrs: AttributeSet?) {
        val ta = context.obtainStyledAttributes(attrs, R.styleable.LineProgresView)
        mTextColor = ta.getColor(R.styleable.LineProgresView_textColor, Color.BLACK)
        mWaveColor = ta.getColor(R.styleable.LineProgresView_waveColor, Color.BLUE)
        mTextSize = ta.getDimensionPixelSize(
            R.styleable.LineProgresView_textSize, sp2px(
                DEFAULT_TEXT_SIZE
            )
        )
        mStrokeWidth= ta.getDimensionPixelOffset(
            R.styleable.LineProgresView_strokeWidth, sp2px(
                DEFAULT_HEIGHT
            )
        )
        mRadius= ta.getDimensionPixelOffset(
            R.styleable.LineProgresView_radius, sp2px(
                DEFAULT_HEIGHT
            )
        )
        mTextStyle = ta.getInt(
            R.styleable.LineProgresView_textStyle, 0
        )

        mProgress = ta.getInteger(R.styleable.LineProgresView_progress, 0)
        ta.recycle()
    }



    private fun initPaint() {
        mProgressPaint = Paint()
        mProgressPaint!!.color = mWaveColor
        mProgressPaint!!.style = Paint.Style.FILL
        mProgressPaint!!.isAntiAlias = true
        mProgressPaint!!.setStrokeWidth(mStrokeWidth.toFloat());


        mTextPaint = Paint(Paint.ANTI_ALIAS_FLAG)
        mTextPaint!!.color = mTextColor
        mTextPaint!!.textSize = mTextSize.toFloat()
        var font = Typeface.create(Typeface.SANS_SERIF,  mTextStyle)
        mTextPaint!!.setTypeface(font)
        mTextRect = Rect()

    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        //宽
        val measureWidth = MeasureSpec.getSize(widthMeasureSpec)
        //高
        val measureHeight = MeasureSpec.getSize(heightMeasureSpec)
        if (measureWidth != 0 && measureHeight != 0) {
            viewWidth = measureWidth
            viewHeight = measureHeight
        }
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        var progres = "$mProgress%"
        mTextPaint!!.getTextBounds(progres, 0, progres.length, mTextRect)
        canvas.drawText(
            progres, (viewWidth - mTextRect!!.width()).toFloat(), (viewHeight+mTextRect!!.height())/2.toFloat(), mTextPaint!!
        )
        //绘制直线
        val fl = ((viewWidth - 1.1*mTextRect!!.width())*progress/100).toFloat()
        var rectf = RectF(0f, (viewHeight-mStrokeWidth)/2.toFloat(), fl, (viewHeight+mStrokeWidth)/2.toFloat())
        canvas.drawRoundRect(rectf, mRadius.toFloat(),mRadius.toFloat(),mProgressPaint!!);
    }



    /**
     * 设置字体的颜色
     *
     * @param color
     */
    fun setTextColor(color: Int) {
        mTextPaint!!.color = color
    }

    /**
     * 设置进度的颜色
     *
     * @param color
    */
    fun setProgressColor(color: Int) {
    mProgressPaint!!.color = color
    }


    /**
     * 获取当前进度
     *
     * @return
     */
    /**
     * 设置进度
     *
     * @param progress
     */
    var progress: Int
        get() = mProgress
        set(progress) {
            mProgress = progress
            postInvalidate()
        }

    //dp to px
    protected fun dp2px(dpval: Int): Int {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_DIP,
            dpval.toFloat(),
            resources.displayMetrics
        ).toInt()
    }

    //sp to px
    protected fun sp2px(dpval: Int): Int {
        return TypedValue.applyDimension(
            TypedValue.COMPLEX_UNIT_SP,
            dpval.toFloat(),
            resources.displayMetrics
        ).toInt()
    }

    companion object {

        /**
         * 默认的进度字体大小
         */
        private const val DEFAULT_TEXT_SIZE = 10

        /**
         * 默认进度条高度
         */
        private const val DEFAULT_HEIGHT = 5
    }

    init {
        getAttrs(attrs)
        initPaint()
    }
}